package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "usernameIsExist" , urlPatterns = {"/usernameIsExist"})
public class UsernameExistServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        //调用service，dao去查询username是否可以用

        resp.setContentType("text/plain");
//        resp.setHeader("Access-Control-Allow-Origin","http://localhost:5501");
        resp.setHeader("Access-Control-Allow-Origin","*");

        /*查找有无相同的用户名*/
        AccountService accountService = new AccountService();
        Account loginAccount = accountService.getAccountByUsername(username);
        PrintWriter out = resp.getWriter();
        if(loginAccount == null){
            out.print("Not Exist");
        }else {
            out.print("Exist");
        }

    }
}
